<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/30
 * Time: 11:13
 */

namespace app\index\model;
use think\Model;
use think\Db;
use dysms\Dysms;
class SmsLogModel extends Model
{
    // 确定链接表名
    protected $table = 'snake_sms_log';
    public function addSmsLog($param){
        try{
            $result = $this->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, $result, '保存成功');
            }
        }catch (\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function findSmsLog($param){
        return $this->where(array('mobile'=>$param['mobile'],'session_id'=>$param['session_id']))->find();
    }

    public function getSmsTemplateByScene($scene){
        return Db::name('sms_template')->where('send_scene', $scene)->find();
    }
    public function sendMobileVerifyCode($param){
        //短信场景
        if(!isset($param['mobile']) ){
            return msg(-1, '', '手机号参数错误');
        }elseif(!check_mobile($param['mobile'])){
            return msg(-1, '', '手机号参数错误');
        }
        $scene='用户注册';
        if(isset($param['type'])){
            switch ($param['type']){
                case 1:
                    $scene='用户注册';
                    break;
                case 2:
                    $scene='修改密码';
                    break;
                case 3:
                    $scene='修改支付密码';
                    break;
                case 4:
                    $scene='绑定手机号';
                    break;
                case 5:
                    $scene='快捷登录';
                    break;
                case 6:
                    $scene='实名认证';
                    break;
                case 7:
                    $scene='在线申请';
                    break;
            }
        }
        $smsTemplate=$this->getSmsTemplateByScene($scene);
        $session_id=md5(time().mt_rand(1,999999999));
        $code = rand(100000, 999999);
        $param=array('mobile'=>$param['mobile'],'sms_sign'=>$smsTemplate['sms_sign'],'sms_tpl_code'=>$smsTemplate['sms_tpl_code'],'code'=>$code);
        $response=Dysms::sendSms($param);
        if($response->Code=='OK'){
            $data=array('mobile'=>$param['mobile'],'session_id'=>$session_id,'code'=>$code,'status'=>1,'scene'=>$scene,'addtime'=>time());
            $smsLogModel=new SmsLogModel();
            $smsLogModel->addSmsLog($data);
            return msg(1,array('mobile'=>$data['mobile'],'session_id'=>$session_id) , '验证码发送成功');
        }else{
            $data=array('mobile'=>$param['mobile'],'session_id'=>$session_id,'code'=>$code,'status'=>1,'scene'=>$scene,'add_time'=>time(),'error_msg'=>$response->Code);
            $smsLogModel=new SmsLogModel();
            $smsLogModel->addSmsLog($data);
            return msg(-2,array('mobile'=>$data['mobile'],'session_id'=>$session_id) , '验证码发送失败,失败原因'.$response->Code);
        }
    }
}